package com.faxsun.web.controller.admin;

import java.util.ArrayList;
import java.util.List;

import mblog.core.pojos.Comment;
import mblog.core.utils.BeanMapUtils;
import mblog.web.controller.BaseController;
import mtons.modules.pojos.Data;
import mtons.modules.pojos.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faxsun.core.service.CommentService;
import com.faxsun.pojos.view.CommentTable;
import com.faxsun.web.controller.admin.url.AdminViewConstants;

/**
 * 评论管理
 * 
 * @author songdragon 2015年5月20日
 *
 */
@Controller
@RequestMapping(value = "/admin/comment")
public class CommentManageController extends BaseController {

	@Autowired
	CommentService commentService;

	@RequestMapping(value = "/list")
	public String list(
			@RequestParam(value = "status", required = false) Integer status,
			@RequestParam(value = "inboxAdd", required = false) Integer inboxAdd,
			ModelMap model) {
		String url = "/admin/comment/data-url";
		if (status != null) {
			url += "?status=" + status;
			model.addAttribute("status", status);
		}
		if(inboxAdd != null){
		   if(inboxAdd == Data.CODE_SUCCESS){
	           model.addAttribute("inboxAdd", "success");		       
		   }else{
               model.addAttribute("inboxAdd", "fail"); 
		   }
		}
		model.addAttribute("url", url);
		model.addAttribute(AdminViewConstants.VIEW,
				AdminViewConstants.VIEW_COMMENT_LIST);

		return AdminViewConstants.VIEW_ADMIN_LAYOUT;
	} 
	
	@RequestMapping(value = "/inbox/add", method = RequestMethod.GET)
    public String view(ModelMap model) {
        model.addAttribute(AdminViewConstants.VIEW, AdminViewConstants.VIEW_COMMENT_ADD);
        return AdminViewConstants.VIEW_ADMIN_LAYOUT;
    }

	/**
	 * JSON获取评论列表数据{@link Comment}
	 * 
	 * @param status
	 * @param pageNo
	 * @param max
	 * @param search
	 * @param field
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/data-url", method = RequestMethod.GET)
	public @ResponseBody CommentTableJSON data(
			@RequestParam(value = "status", required = false, defaultValue = "-1") Integer status,
			@RequestParam(value = "toId", required = false, defaultValue = "-1") Integer toId,
			@RequestParam(value = "type", required = false, defaultValue = "-1") Integer type,
			@RequestParam(value = "offset", required = false) Integer pageNo,
			@RequestParam(value = "limit", required = false) Integer max,
			@RequestParam(value = "search", required = false) String search,
			@RequestParam(value = "sort", required = false) String field,
			ModelMap model) {
		// 参数校验及转换
		if (pageNo == null) {
			pageNo = 1;
		}
		if (max == null) {
			max = 10;
		}
		pageNo = pageNo / max + 1;
		Page page = wrapPage(pageNo);
		page.setMaxResults(max);

		// TODO:按字段排序
		if (StringUtils.isEmpty(search)) {
			this.commentService.paging(page, toId, type, status);
		} else {
			// TODO:搜索结果
		}
		CommentTableJSON ret = new CommentTableJSON();
		ret.total = page.getTotalCount();
		ArrayList<CommentTable> table = new ArrayList<CommentTable>();
		for (Comment comment : (List<Comment>) page.getResults()) {
			table.add(BeanMapUtils.copy(comment));
		}
		ret.rows = table;
		return ret;
	}
	
	
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
	public @ResponseBody Data delete(@PathVariable Long id) {
		Data data = Data.failure("操作失败");
		if (id != null) {
			try {
				this.commentService.delete(id);
				data = Data.success("操作成功");
			} catch (Exception e) {
				data = Data.failure(e.getMessage());
			}
		}
		return data;
	}
}

class CommentTableJSON{
	public int total;
	public List<CommentTable> rows;
	
}
